package com.donfu.system.service.house.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.donfu.system.domain.dto.HouseSpecParamDTO;
import com.donfu.system.domain.dto.HouseSpecParamDTO;
import com.donfu.system.domain.house.HouseSpecParam;
import com.donfu.system.mapper.house.HouseSpecParamMapper;
import com.donfu.system.service.house.IHouseSpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品规格参数Service业务层处理
 * 
 * @author donfu
 * @date 2021-12-06
 */
@Service
public class HouseSpecParamServiceImpl implements IHouseSpecParamService
{
    @Autowired
    private HouseSpecParamMapper houseSpecParamMapper;

    /**
     * 查询商品规格参数
     * 
     * @param id 商品规格参数主键
     * @return 商品规格参数
     */
    @Override
    public HouseSpecParam selectHouseSpecParamById(Long id)
    {
        return houseSpecParamMapper.selectHouseSpecParamById(id);
    }

    /**
     * 查询商品规格参数列表
     * 
     * @param houseSpecParam 商品规格参数
     * @return 商品规格参数
     */
    @Override
    public List<HouseSpecParam> selectHouseSpecParamList(HouseSpecParam houseSpecParam)
    {
        return houseSpecParamMapper.selectHouseSpecParamList(houseSpecParam);
    }

    /**
     * 查询商品规格参数列表以树形结构返回
     *
     * @param goodsId 商品信息ID
     * @return 查询商品规格参数列表以树形结构返回
     */
    public List<HouseSpecParamDTO> getSpecParamTree(Long goodsId){

        // 1.查询出所有的分类
        List<HouseSpecParamDTO> specParamDTOS = houseSpecParamMapper.getSpecParamTree(goodsId);
        // 2.组装成父子的树形结构
        List<HouseSpecParamDTO> specParamTrees = new ArrayList<>();
        for (HouseSpecParamDTO specParamDTO : specParamDTOS) {
            // 找到所有的一级分类
            if (specParamDTO.getParentId() == 0){
                specParamTrees.add(specParamDTO);
            }
        }
        // 通过父级规格参数查找父级下所有子规格参数,然后将其添加到父级对象的子规格参数集合中
        for (HouseSpecParamDTO specParamTree : specParamTrees) {
            specParamTree.setChildren(getChildrens(specParamTree,specParamDTOS));
        }

        //排序
        specParamTrees.sort(new Comparator<HouseSpecParamDTO>() {
            @Override
            public int compare(HouseSpecParamDTO o1, HouseSpecParamDTO o2) {
                return (int) ((o1.getSort() == null ? 0 : o1.getSort()) - (o2.getSort() == null ? 0 : o2.getSort()));
            }
        });

        return specParamTrees;
    }
    
    /**
     * 递归查找所有的下级分类
     * 在这一级别的分类中找下级分类
     *
     * @param root 当前级别的分类
     * @param all  全部分类
     * @return 下一级分类
     */
    private List<HouseSpecParamDTO> getChildrens(HouseSpecParamDTO root, List<HouseSpecParamDTO> all) {
        List<HouseSpecParamDTO> children = new ArrayList<>();
        for (HouseSpecParamDTO a : all) {
            if (a.getParentId().equals(root.getId())) {
                a.setChildren(getChildrens(a, all));
                children.add(a);
            }
        }
        children.sort(new Comparator<HouseSpecParamDTO>() {
            @Override
            public int compare(HouseSpecParamDTO o1, HouseSpecParamDTO o2) {
                return (int) ((o1.getSort() == null ? 0 : o1.getSort()) - (o2.getSort() == null ? 0 : o2.getSort()));
            }
        });
        return children;
    }


    /**
     * 新增商品规格参数
     * 
     * @param houseSpecParam 商品规格参数
     * @return 结果
     */
    @Override
    public int insertHouseSpecParam(HouseSpecParam houseSpecParam)
    {
        houseSpecParam.setCreateDate(new Date());
        return houseSpecParamMapper.insertHouseSpecParam(houseSpecParam);
    }

    /**
     * 修改商品规格参数
     * 
     * @param houseSpecParam 商品规格参数
     * @return 结果
     */
    @Override
    public int updateHouseSpecParam(HouseSpecParam houseSpecParam)
    {
        houseSpecParam.setUpdateDate(new Date());
        return houseSpecParamMapper.updateHouseSpecParam(houseSpecParam);
    }

    /**
     * 批量删除商品规格参数
     * 
     * @param ids 需要删除的商品规格参数主键
     * @return 结果
     */
    @Override
    public int deleteHouseSpecParamByIds(Long[] ids)
    {
        return houseSpecParamMapper.deleteHouseSpecParamByIds(ids);
    }

    /**
     * 删除商品规格参数信息
     * 
     * @param id 商品规格参数主键
     * @return 结果
     */
    @Override
    public int deleteHouseSpecParamById(Long id)
    {
        return houseSpecParamMapper.deleteHouseSpecParamById(id);
    }
}
